﻿KHACHHANG = function () {
    
}

KHACHHANG.columns = new Array("Mã Khách Hàng", "Tên Khách Hàng", "Mã Đơn Vị");
KHACHHANG.fieldText = new Array("MaKhachHang", "TenKhachHang", "MaDonVi");
KHACHHANG.MaKhachHang = -1;
var numberPage = 1;
KHACHHANG.isInsert = 0;
KHACHHANG.arrays = new Array();
KHACHHANG.Init = function () {
    KHACHHANG.arrays = new Array();
    var obj;
    obj = new CheckField("TenKhachHang", 1, "ErrTenKhachHang");
    KHACHHANG.Add(obj);    
}

KHACHHANG.Add = function (obj) {
    if (KHACHHANG.arrays.contains(obj) == false) {
        KHACHHANG.arrays.push(obj);
    }
}
KHACHHANG.Insert = function () {
    var serviceUrl = Constants.service + "insertTblKhachHangs";
    if (Constants.OnSubmit(KHACHHANG.arrays) == false) {
        return false;
    }
    var data = {
        MaKhachHang: $("#MaKhachHang").val(),
        TenKhachHang: $("#TenKhachHang").val(),
        MaDonVi: $("#MaDonVi").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            KHACHHANG.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

KHACHHANG.Delete = function (id) {    
    var data = {
        MaKhachHang: id
    };
    var json = JSON.stringify({ item: data });
    a = confirm(Constants.messDelete);
    if (a == false) {
        return;
    }
    var serviceUrl = Constants.service + "deleteTblKhachHangs";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (data) {
            KHACHHANG.GetData();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

KHACHHANG.UpdateItem = function () {
    var serviceUrl = Constants.service + "updateTblKhachHangs";
    if (Constants.OnSubmit(KHACHHANG.arrays) == false) {
        return false;
    }
    var data = {
        MaKhachHang: $("#MaKhachHang").val(),
        TenKhachHang: $("#TenKhachHang").val(),
        MaDonVi: $("#MaDonVi").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            KHACHHANG.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

KHACHHANG.CreateTable = function (r, nbpage, data) {
    var myTable = KHACHHANG.TitleColumns(KHACHHANG.columns);
    myTable += KHACHHANG.DataColumnsOftr(data, r, nbpage);
    myTable += "</table>";
    $("#div_list").children().remove();
    $("#div_list").append(myTable);
    $("#div_content").show();
}

KHACHHANG.GetData = function () {
    KHACHHANG.MaKhachHang = -1;
    var serviceUrl = Constants.service + "getAllTblKhachHangs";
    var data = { url: window.location.host };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var results = msg.getAllTblKhachHangsResult;
            var dataTables = results;
            KHACHHANG.CreateTable(Constants.rows, numberPage, dataTables);
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

KHACHHANG.TitleColumns = function (_columns) {
    var myColumns = "<table class=\"rounded-corner\">";
    myColumns += "<thead><tr>";
    for (var i = 0; i < _columns.length; i++) {
        myColumns += "<th class=\"rounded\" scope=\"col\">" + _columns[i] + "</th>";
    }
    myColumns += "<th class=\"rounded\" scope=\"col\">Edit</th>";
    myColumns += "<th class=\"rounded-q4\" scope=\"col\">Delete</th>";
    myColumns += "</tr></thead>";
    var n = _columns.length + 1;
    myColumns += "<tfoot><tr><td class=\"rounded-foot-left\" colspan=\"" + n + "\"><em> </em></td>";
    myColumns += "<td class=\"rounded-foot-right\">&nbsp;</td>";
    myColumns += "</tr></tfoot>";
    return myColumns;
}

KHACHHANG.DataColumnsOftr = function (data, rs, _numberPage) {
    var myDataColumns = "<tbody>";
    var n = data.length;
    var j = (_numberPage - 1) * rs;
    var r = rs;
    for (var i = j; i < r && i < n; i++) {
        var id = data[i].MaKhachHang;
        myDataColumns += "<tr onclick='return KHACHHANG.OnClick(" + id + ")' id=\"" + id + "\">";      
        myDataColumns += "<td>" + data[i].MaKhachHang + "</td>"
        myDataColumns += "<td>" + data[i].TenKhachHang + "</td>"
        myDataColumns += "<td>" + data[i].TenDonVi + "</td>"
        
        myDataColumns += "<td><img onclick='return KHACHHANG.EditItem(" + id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/user_edit.png\"></td>";
        myDataColumns += "<td><a class=\"ask\" ><img onclick='return KHACHHANG.Delete("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/trash.png\"></a></td>";
    }
    myDataColumns += "</tbody>";
    return myDataColumns;
}

KHACHHANG.OnClick = function (id) {
    KHACHHANG.MaKhachHang = id;
    $(".active").removeClass();
    $("#" + id).addClass("active");
}

KHACHHANG.OnClickEdit = function () {
    if (KHACHHANG.MaKhachHang == -1) {
        alert(Constants.selectEdit);
    }
    else
        KHACHHANG.EditItem(KHACHHANG.MaKhachHang);
}

KHACHHANG.OnClickDelete = function () {
    if (KHACHHANG.MaKhachHang == -1) {
        alert(Constants.selectDelete);
    }
    else
        KHACHHANG.Delete(KHACHHANG.MaKhachHang);
}
KHACHHANG.EditItem = function (id) {    
    var temp_item = "Modules/kh/Edit";   
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Sửa" });
            KHACHHANG.GetItem(id);
            Constants.GetDonVi("MaDonVi");
        }
    });
}

KHACHHANG.GetItem = function (id) {
    var serviceUrl = Constants.service + "getItemByIdTblKhachHang";
    var data = { MaKhachHang: id };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getItemByIdTblKhachHangResult;
            if (result != null && result != undefined) {
                $("#MaKhachHang").val(result.MaKhachHang);
                $("#TenKhachHang").val(result.TenKhachHang);
                $("#MaDonVi").val(result.MaDonVi);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

KHACHHANG.Clear = function () {
    Constants.clearAll(KHACHHANG.fieldText);
}

KHACHHANG.AddNewItem = function () {
    var temp_item = "Modules/kh/AddNew";    
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Thêm Mới" });
            Constants.GetDonVi("MaDonVi");
            KHACHHANG.getMaKhachHang();
        }
    });
}

KHACHHANG.getMaKhachHang = function () {
    var serviceUrl = Constants.service + "getMaTblKhachHang";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        //data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getMaTblKhachHangResult;
            if (result != undefined && result != null) {
                $("#MaKhachHang").val(result);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

KHACHHANG.Enter = function () {
    $("#MaKhachHang").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $("#TenKhachHang").focus();
        }
    });
    $("#TenKhachHang").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $("#MaDonVi").focus();
        }
    });
}

KHACHHANG.Search = function () {
    var serviceUrl = Constants.service + "getFindTblKhachHangs";
    var data = {
        value: $("#search").val()
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl,
        data: json,
        contentType: "application/json",
        dataType: "json",
        cache: false,
        success: function (msg) {
            var results = msg.getFindTblKhachHangsResult;
            if (results != null && results.length > 0) {
                KHACHHANG.CreateTable(Constants.rows, numberPage, results);
            }
            else {
                $("#div_list").children().remove();
                $("#div_list").html(Constants.find);
            }
        }
    });
}

$(document).ready(function () {
    KHACHHANG.GetData();
    KHACHHANG.Init();
    $("#search").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            KHACHHANG.Search();
        }
    });
    $("#search_on").click(function (event) {
        event.preventDefault();
        KHACHHANG.Search();
    });
});